<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use Mail;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Requests\FatiePostRequest;
use App\Http\Requests;

use App\Http\Controllers\Controller;

class TieziController extends Controller
{
	public function getIndex(Request $request)
    {
    	// echo '000';die;
        $num = $request->input('num',5);
        //判断用户是否查询
        if(!empty($request->input('keywords'))){
            $tiezi = DB::table('tiezi')
                ->join('plate','tiezi.plate_id','=','plate.id')
                ->join('user','tiezi.uid','=','user.id')
                ->select('tiezi.*','plate.plate_name','user.username')
                ->where('title','like','%'.$request->input('keywords').'%')
                ->orderBy('id','asc')
                ->paginate($num);
        }else{
            //查询所有帖子
            $tiezi = DB::table('tiezi')
                ->join('plate','tiezi.plate_id','=','plate.id')
                ->join('user','tiezi.uid','=','user.id')
                ->select('tiezi.*','plate.plate_name','user.username')
                ->orderBy('id','asc')
                ->paginate($num);
        }
        
        // dd($goods);
        //解析模板
        return view('admin.tiezi.index',['tiezi'=>$tiezi,'request'=>$request->all()]);

    }
    //删除帖子
    public function postDelete(Request $request){

        //获取id
         $id = $request->input('id');
        $aid = DB::table('answer')->where('tid',$id)->delete();
         //执行删除
        $res = DB::table('tiezi')->where('id',$id)->delete();
        //判断
        if($res){
            //删除成功
            echo 1;
        }else{
            //删除失败
            echo 0;
        }
    }

    //主贴修改

        public function getEdit($id)
    {
        $tiezi = DB::table('tiezi')->where('id',$id)->first();
        return view('admin.tiezi.edit',['tiezi'=>$tiezi]);
    }

     public function postUpdate(Request $request)
    {

        $this->validate($request, [
        'title' => 'required',
        'content' => 'required',
       
        ],[
            'title.required'=>'帖子标题不能为空',
            'content.required'=>'帖子内容不能为空',
            
        ]);
        $id = $request->input('id');
        // dd($id);
        //上面验证都成功
        //提取部分参数
        $data = $request->only(['title','content']);
        // dd($data);
        //执行数据修改
         $res = DB::table('tiezi')->where('id',$id)->update($data);

        if($res){
            return redirect('/admin/tiezi/index')->with('success','帖子修改成功');
        }else{
            return back()->with('error','帖子修改失败');
        } 
        
   } 



    //前台发帖
    public function postFatie(Request $request)
    {
        // dd($request->all());
        $plate_id = $request->input('id');
        // dd($plate_id);
        // echo '111';
        //判断是否登录
        if(session('id')){
            // $id = session('id');

             $menu = HomeListController::menu(0);
            //解析模板
            return view('home.community.fatie',['plate_id'=>$plate_id,'menu'=>$menu]);
        }else{
            return redirect('/login');
            // return view('home.community.list');
        }

        
    }
   
    public function postFasong(FatiePostRequest $request){ 
        //上面验证都成功
        //提取数据 
        $id = session('id');
        // dd($id);
        // dd($request->all());
        // $res = DB::table('tiezi')->insert(['uid'=>$id,'title'=>'yogndfds']);
      

        $data = $request->only('title','content');

        //获取版块id
        $plate_id = $request->input('plate_id');
        // dd($plate_id);
        $data['plate_id'] = $plate_id;
        // dd($data['plate_id']);

        $data['ptime'] = date('Y-m-d H:i:s',time());
        // dd($data['ptime']);
        //获取发帖人
        $user = DB::table('user')->where('id',$id)->first();
        $name = $user->username;
        $data['uid'] = $user->id;
        
        //获取分区id
        // $partition = DB::table('partition')->where('user_id',$id)->first();
        // $partition_id = $partition->id;
        // dd($partition_id);
        // 获取版块id
        // $plate = DB::table('plate')->where('partition_id',$partition_id)->first();
        // $data['plate_id'] = $plate->id;
        // dd($ptime);
        // dd($plate_id);
  
        //执行数据插入操作
        $id = DB::table('tiezi')->insert($data);
        // $tiezi = DB::table('tiezi')
        //         ->join('user','tiezi.uid' ,'=','user.id')
        //         ->select('tiezi.*','user.username')

        //         ->get();
        // dd($id);
        if($id){

            // return redirect('/community/list',['id'=>$id]);
            // return redirect()->action('CommunityController@getList', [$partition_id]);
            return redirect()->action('CommunityController@getList', [$plate_id]);
        }else{
            return back()->with('error','帖子发表失败');  
        }
    }
    

    
} 

